package com.authine.cloudpivot.ext.util;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.authine.cloudpivot.ext.conf.ApplicationConf;
import com.authine.cloudpivot.ext.domain.dto.SMSTemplateParamsDto;
import com.authine.cloudpivot.ext.enums.SMSActionEnum;
import com.authine.cloudpivot.ext.enums.SMSTemplateCodeEnum;

public class SendSmsUtils {

	static final String regionId= ApplicationConf.REGION_ID;
	//短信API产品域名
	static final String accessKeyId= ApplicationConf.ACCESS_KEY_ID;
	//短信API产品域名
	static final String accessKeySecret= ApplicationConf.ACCESS_KEY_SECRET;
	//短信API产品域名
	static final String domain = ApplicationConf.SYS_DOMAIN;
	//短信API产品域名
	static final String version = ApplicationConf.SYS_VERSION;
	//短信API产品签名1
	static final String signName1 = ApplicationConf.SIGN_NAME1;
	//短信API产品签名2
	static final String signName2 = ApplicationConf.SIGN_NAME2;
    
	/**
	 * 消息发送
	 *
	 * @param phone        收信号码
	 * @param message      信息内容对象
	 * @param templateCode 信息模板ID枚举类：SMSTemplateCodeEnum
	 * @return
	 */
	public static Object sendSingleSms(String phone, SMSTemplateParamsDto message, String templateCode) {

		CommonRequest request = new CommonRequest();

		if (SMSTemplateCodeEnum.RUZHU_SUCCESSS.text.equals(templateCode)) {//入驻成功反馈
			if (message.getName() == null || "".equals(message.getName())) throw new RuntimeException("姓名不能为空");
			request.putQueryParameter("TemplateParam", "{\"name\":\"" + message.getName() + "\"}");
		} else if (SMSTemplateCodeEnum.RUZHU_FEEDBACK.text.equals(templateCode)) {//报价通知
			if (message.getInquiryNo() == null || "".equals(message.getInquiryNo()))
				throw new RuntimeException("询价单号不能为空");
			request.putQueryParameter("TemplateParam", "{\"code\":\"" + message.getInquiryNo() + "\"}");
		} else if (SMSTemplateCodeEnum.BAOJIA_NOTIFY.text.equals(templateCode)) {//入驻反馈
        	if(message.getName()==null || "".equals(message.getName())) throw new RuntimeException("姓名不能为空");
        	request.putQueryParameter("TemplateParam", "{\"name\":\""+message.getName()+"\"}");
        }else if(SMSTemplateCodeEnum.FANKUI_NOTIFY.text.equals(templateCode)) {//反馈通知
        	if(message.getFeedbackNo()==null || "".equals(message.getFeedbackNo())) throw new RuntimeException("反馈单号不能为空");
        	request.putQueryParameter("TemplateParam", "{\"code\":\""+message.getFeedbackNo()+"\"}");
        }else if(SMSTemplateCodeEnum.ORDER_NOTIFY.text.equals(templateCode)) {//订单通知
        	if(message.getOrderNo()==null || "".equals(message.getOrderNo())) throw new RuntimeException("订单号不能为空");
        	request.putQueryParameter("TemplateParam", "{\"code\":\""+message.getOrderNo()+"\"}");
        }else if(SMSTemplateCodeEnum.HUODONG_AUTH_CODE.text.equals(templateCode)) {//活动确认验证码
        	if(message.getCode()==null || "".equals(message.getCode())) throw new RuntimeException("验证码不能为空");
        	if(message.getProduct()==null || "".equals(message.getProduct())) throw new RuntimeException("环境名称不能为空");
        	if(message.getActivitiName()==null || "".equals(message.getActivitiName())) throw new RuntimeException("活动名称不能为空");
        	request.putQueryParameter("TemplateParam", "{\"code\":\""+message.getCode()+"\",\"product\":\""+message.getProduct()+"\",\"item\":\""+message.getActivitiName()+"\"}");
        }else if(SMSTemplateCodeEnum.ALTER_PASSWORD_AUTH_CODE.text.equals(templateCode)) {//修改密码验证码
        	if(message.getCode()==null || "".equals(message.getCode())) throw new RuntimeException("验证码不能为空");
        	if(message.getProduct()==null || "".equals(message.getProduct())) throw new RuntimeException("环境名称不能为空");
        	request.putQueryParameter("TemplateParam", "{\"code\":\""+message.getCode()+"\",\"product\":\""+message.getProduct()+"\"}");
        }else if(SMSTemplateCodeEnum.LOGIN_AUTH_CODE.text.equals(templateCode)) {//登陆确认验证码
        	if(message.getCode()==null || "".equals(message.getCode())) throw new RuntimeException("验证码不能为空");
        	if(message.getProduct()==null || "".equals(message.getProduct())) throw new RuntimeException("环境名称不能为空");
        	request.putQueryParameter("TemplateParam", "{\"code\":\""+message.getCode()+"\",\"product\":\""+message.getProduct()+"\"}");
        }else if(SMSTemplateCodeEnum.REGISTER_AUTH_CODE.text.equals(templateCode)) {//用户注册验证码
        	if(message.getCode()==null || "".equals(message.getCode())) throw new RuntimeException("验证码不能为空");
        	if(message.getProduct()==null || "".equals(message.getProduct())) throw new RuntimeException("环境名称不能为空");
        	request.putQueryParameter("TemplateParam", "{\"code\":\""+message.getCode()+"\",\"product\":\""+message.getProduct()+"\"}");
        }else if(SMSTemplateCodeEnum.IDENTIFY_AUTH_CODE.text.equals(templateCode)) {//身份验证验证码
        	if(message.getCode()==null || "".equals(message.getCode())) throw new RuntimeException("验证码不能为空");
        	if(message.getProduct()==null || "".equals(message.getProduct())) throw new RuntimeException("环境名称不能为空");
        	request.putQueryParameter("TemplateParam", "{\"code\":\""+message.getCode()+"\",\"product\":\""+message.getProduct()+"\"}");
        }else if(SMSTemplateCodeEnum.SMS_TEST.text.equals(templateCode)) {//短信测试
        	if(message.getCode()==null || "".equals(message.getCode())) throw new RuntimeException("验证码不能为空");
        	if(message.getProduct()==null || "".equals(message.getProduct())) throw new RuntimeException("环境名称不能为空");
        	request.putQueryParameter("TemplateParam", "{\"customer\":\""+message.getName()+"\"}");
        }else if(SMSTemplateCodeEnum.INFO_ALTER_AUTH_CODE.text.equals(templateCode)) {//信息变更验证码
        	if(message.getCode()==null || "".equals(message.getCode())) throw new RuntimeException("验证码不能为空");
        	if(message.getProduct()==null || "".equals(message.getProduct())) throw new RuntimeException("环境名称不能为空");
        	request.putQueryParameter("TemplateParam", "{\"code\":\""+message.getCode()+"\",\"product\":\""+message.getProduct()+"\"}");
        }else if(SMSTemplateCodeEnum.EXCEPTION_AUTH_CODE.text.equals(templateCode)) {//登陆异常验证码
        	if(message.getCode()==null || "".equals(message.getCode())) throw new RuntimeException("验证码不能为空");
        	if(message.getProduct()==null || "".equals(message.getProduct())) throw new RuntimeException("环境名称不能为空");
        	request.putQueryParameter("TemplateParam", "{\"code\":\""+message.getCode()+"\",\"product\":\""+message.getProduct()+"\"}");
        }
		
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(domain);
        request.setSysVersion(version);
        request.setSysAction(SMSActionEnum.SENDSMS.text);
        request.putQueryParameter("PhoneNumbers", phone);
		request.putQueryParameter("SignName", signName2);
        request.putQueryParameter("TemplateCode", templateCode);
        
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
		IAcsClient client = new DefaultAcsClient(profile);
        
        CommonResponse response;
		try {
			response = client.getCommonResponse(request);
			return JSON.parseObject(response.getData());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static void main(String[] args) {
		SMSTemplateParamsDto entity = new SMSTemplateParamsDto();
		entity.setCode("110112");
		entity.setProduct("test");
		sendSingleSms("13105812875",entity ,SMSTemplateCodeEnum.INFO_ALTER_AUTH_CODE.text);
	}
	
}
